

public class SimpleNode implements Node {
  protected Node parent;
  protected Node[] children;
  protected int id;
  protected Object value;
  protected JavaRolePreCompiler parser;
  protected Token firstToken;
  protected Token lastToken;


  private boolean printable;
	
	
  public SimpleNode(int i) {
    id = i;
    printable=true;
  }
  
  public void notPrintable(){
	  printable=false;
  }
  
  public boolean isPrintable(){
	  return printable;
  }

  public SimpleNode(JavaRolePreCompiler p, int i) {
    this(i);
    parser = p;
    printable=true;
  }

  public void jjtOpen() {
	  
  }

  public void jjtClose() {
  }
  
  public void jjtSetParent(Node n) { parent = n; 
	}
  public Node jjtGetParent() { return parent; }

  public void jjtAddChild(Node n, int i) {
    if (children == null) {
      children = new Node[i + 1];
    } else if (i >= children.length) {
      Node c[] = new Node[i + 1];
      System.arraycopy(children, 0, c, 0, children.length);
      children = c;
    }
    children[i] = n;
  }

  public Node jjtGetChild(int i) {
    return children[i];
  }
  
  public Node[] getChildren(){
	  return children;
  }

  public int jjtGetNumChildren() {
    return (children == null) ? 0 : children.length;
  }

  public void jjtSetValue(Object value) { this.value = value; }
  public Object jjtGetValue() { return value; }

  public Token jjtGetFirstToken() { return firstToken; }
  public void jjtSetFirstToken(Token token) { this.firstToken = token; }
  public Token jjtGetLastToken() { return lastToken; }
  public void jjtSetLastToken(Token token) { this.lastToken = token; }

  /** Accept the visitor. **/
  public Object jjtAccept(JavaRolePreCompilerVisitor visitor, Object data) {
    return visitor.visit(this, data);
  }

  /** Accept the visitor. **/
  public Object childrenAccept(JavaRolePreCompilerVisitor visitor, Object data) {
    if (children != null) {
      for (int i = 0; i < children.length; ++i) {
        children[i].jjtAccept(visitor, data);
      }
    }
    return data;
  }

  /* You can override these two methods in subclasses of SimpleNode to
     customize the way the node appears when the tree is dumped.  If
     your output uses more than one line you should override
     toString(String), otherwise overriding toString() is probably all
     you need to do. */

  public String toString() { return JavaRolePreCompilerTreeConstants.jjtNodeName[id]; }
  public String toString(String prefix) { return prefix + toString(); }

  /* Override this method if you want to customize how the node dumps
     out its children. */

  public void dump(String prefix) {
    System.out.println(toString(prefix));
    if (children != null) {
      for (int i = 0; i < children.length; ++i) {
  SimpleNode n = (SimpleNode)children[i];
  if (n != null) {
    n.dump(prefix + " ");
  }
      }
    }
  }
  
  public SimpleNode powerClone(){
	  SimpleNode clone = new SimpleNode(parser,id);	  
	  
	  clone.children=children;//.clone();
	  clone.firstToken= firstToken;
	  clone.lastToken=lastToken;//qui forse sarebbe meglio fare lastToken.clone()
	  return clone;  
  }
  
  public String toPowerString(){
	  String sought="";
	  Token cursor = jjtGetFirstToken();
		Token lastToken = jjtGetLastToken();
		
		if(this instanceof ASTModifiers){//modifiers are always special
			String[] modifiers = {"abstract", "final", "native", "private", "protected", "public", "static", "strictfp",
				"synchronized", "transient", "volatile"};//beware of alphabetic order!
			//can have a child: annotation()
			if (!(java.util.Arrays.binarySearch(modifiers, jjtGetFirstToken().image) >= 0 || jjtGetNumChildren() > 0)) {
				
				return "" ;
			}
		}
		if (jjtGetNumChildren()>0){
			
			for (Node child : getChildren()){
				//stampo i token prima del figlio
				Token firstOfChild = ((SimpleNode)child).jjtGetFirstToken();
				
				while(cursor!=firstOfChild){
					sought+=" "+cursor.image;
					
					cursor=cursor.next;
				}
				//visito il figlio 
				
				sought+=((SimpleNode)child).toPowerString();
				
				//se non ho token oltre l'ultimo figlio posso uscire
				if (((SimpleNode)child).jjtGetLastToken()==lastToken) return sought;				
				
				//siccome ho ancora da scrivere mi sposto oltre.
				cursor=((SimpleNode)child).jjtGetLastToken().next;
				
			}
		}
		//stampo i token rimanenti
		while(cursor!=lastToken){			
			sought+=" "+cursor.image;
			cursor=cursor.next;
		}
		sought+=" "+cursor.image;//..compreso l'ultimo
		
		return sought;	  
  }
  
  
}
